#include "read_options.h"
namespace po = boost::program_options;
using std::vector;
using std::string;

#include <boost/format.hpp>
		using boost::format;
#include "glob_files.h"
	using glob_files::glob_expand;


//	allele_file=/data/www/18genomes/variants.tables/chr*.alleles.txt
//	call_file=/Net/rabid/data/scratch/xiang/ima_new/magicpool/bcarab_1st/fqs/*.tmp2
//  exclude_regions_gff=/data/www/POOLING/ARABIDOPSIS/FOUNDER/transposons.gff
//  penalty=


bool read_options(prog_options& opt, int argc, char *argv[])
{
	opt.mandatory.add_options()
								("allele_file,a",po::value<vector<string> >(&opt.allele_filenames)
										//->set_name("FILE"),
										,"Alleles in space/tab delimited format. Header on first line.\n"
										 "<chr><pse.bp><bp><nalleles><maf><species 1><...><species N>.\n"
										 "Multiple file names can be specified or glob patterns (*.tmp). " )
								("call_file,c", po::value<vector<string>>(&opt.call_filenames)
										//->set_name("FILE"),
										,"Calls in space/tab delimited format. <chr><pos><ref><alt>. "
											"Multiple file paths can be specified or glob patterns (*.tmp). ")
								("penalty,p",po::value<unsigned>(&opt.penalty)
										//->set_name("FILE"),
										,"Founder (state) change penalty." );


	opt.optional.add_options()
								("exclude_regions_gff,x", po::value<vector<string>>(&opt.exclude_regions_gff_filenames)
										//->set_name("FILE"),
										,"Ignore alleles from masked regions specified in gff format." )
								("exclude_regions,X", po::value<vector<string>>(&opt.exclude_regions_filenames)
										//->set_name("FILE"),
										,"Ignore alleles from masked regions specified in tab delimited format. "
										 "<chr><beg><end>" )
								("diploid,d",po::value<bool>(&opt.diploid)
										->zero_tokens()
										,"Run diploid dynamic programming which allows for heterozygosity.")
								("haploid,H",po::value<bool>(&opt.haploid)
										->zero_tokens()
										,"Run dynamic programming which does not allows for heterozygosity.")
								("homozygous_match,z",po::value<unsigned>(&opt.homozygous_match)
										//->set_name("FILE"),
										->default_value(3)
										,"Score for matches in a homozygous region." )
								("heterozygous_match,Z",po::value<unsigned>(&opt.heterozygous_match)
										->default_value(2)
										//->set_name("FILE"),
										 ,"Score for matches in a heterozygous region." )
								("imputed_output_file,i", po::value<string>(&opt.imputed_output_file_pattern)
										 ->default_value("chr[CONTIG].imputed.txt")
										//->set_name("FILE"),
										,"Pattern to construct file name to hold imputed genotypes for each contig. "
										 "The name of the contig will replace occurences of [CONTIG].")
								("sdp_output_file,s", po::value<string>(&opt.sdp_output_file_pattern)
										 ->default_value("chr[CONTIG].sdp.txt")
										//->set_name("FILE"),
										,"Pattern to construct files names for imputed strain distribution patterns for each contig. "
										 "The name of the actual contig will replace occurences of [CONTIG].")
								("call_file_to_name_regex", po::value<string>(&opt.call_file_to_name_regex)
										 ->default_value(R"(.*?([^/]+)\..*)")
										//->set_name("FILE"),
										,"Regular expression to construct name of animal corresponding to each sequence call file. "
										 "Defaults to file name without path or extension.")
								("founders_output_file,f", po::value<string>(&opt.founders_output_file)
										 ->default_value("combined_segments.txt")
										//->set_name("FILE"),
										,"Holds inferred founder haplotypes for each sequence call file. The tab delimited columns are "
										 "<call name>(see --call_file_to_name_regex)<haploid|diploid> "
										 "<contig> <founder1> <founder2> <from bp> <to bp> <length> <cnt_sites> <mismatches> <transitions> "
										 "<informative> <uninformative> <mismatches per site> <mismatches per bp>.");




	opt.config_file.add_options()
								("allele_file,a",					po::value<vector<string>>(&opt.allele_filenames) 				)
								("call_file,c",						po::value<vector<string>>(&opt.call_filenames) 					)
								("exclude_regions_gff,x",			po::value<vector<string>>(&opt.exclude_regions_gff_filenames)	)
								("exclude_regions,X",				po::value<vector<string>>(&opt.exclude_regions_filenames)		)
								("penalty,p",						po::value<unsigned>(&opt.penalty) 								)
								("homozygous_match,z",				po::value<unsigned>(&opt.homozygous_match)						)
								("heterozygous_match,Z",			po::value<unsigned>(&opt.heterozygous_match)					)
								("diploid,d",						po::value<bool>(&opt.diploid)   								)
								("haploid,H",						po::value<bool>(&opt.haploid)   								)
								("sdp_output_file,s", 				po::value<string>(&opt.sdp_output_file_pattern)					)
								("imputed_output_file,i", 			po::value<string>(&opt.imputed_output_file_pattern) 			)
								("call_file_to_name_regex", 		po::value<string>(&opt.call_file_to_name_regex)					)
								("founders_output_file,f",			po::value<string>(&opt.founders_output_file)					);


	if (!opt.process_command_line(argc, argv))
		return false;

	//
	//	One or the other!
	//
	if (!opt.diploid)
		opt.haploid = true;

	opt.allele_filenames				= glob_expand(opt.allele_filenames);
	if (! opt.allele_filenames.size())
		throw std::logic_error("No matching allele files were found. Please check the glob patterns in --allele_file");
	opt.call_filenames	                = glob_expand(opt.call_filenames);
	if (! opt.call_filenames.size())
		throw std::logic_error("No matching variant call files were found. Please check the glob patterns in --call_file");
	opt.exclude_regions_filenames	    = glob_expand(opt.exclude_regions_filenames);
	opt.exclude_regions_gff_filenames	= glob_expand(opt.exclude_regions_gff_filenames);

	opt.verbose() << "\n\n";
	return true;
}



